/**
 * @author wly
 * @version V1.0.0
 * @date 2023/3/18 20:24
 * https://leetcode.cn/problems/sqrtx/
 */
public class exr_x_sqrt {

    public static int mySqrt(int x) {
        if (x <= 1) return x;
        int left = 1, right = x;
        int ans = -1;
        while (left <= right){
            int mid = left + (right - left) / 2;
            if ((long)mid * mid <= x){
                ans = mid;
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(mySqrt(8));
    }
}
